﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Event_SG_EWDT
{
    public partial class InvitePplToEvent : System.Web.UI.Page
    {
        string eventid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            eventid = Request.QueryString["eventid"];

            if (Session["Member"] == null || eventid == "")
            {
                Session["flag"] = "redirecterror";
                Response.Redirect("~/registration.aspx");
            }
        }

        protected void btnInviteToEvent_Click(object sender, EventArgs e)
        {
            int hasCheckedAtLeastOne = CheckBoxList1.SelectedIndex;
            int error = 0;

            if (tbxInviteToEventEmails.Text == "" && hasCheckedAtLeastOne == -1)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One of more required fields were left empty!');", true);
                return;
            }

            try
            {
                Event eve = DBMANAGER.RetrieveEvent(eventid);

                if (eve.EventId != null)
                {

                    for (int i = 0; i < CheckBoxList1.Items.Count; i++)
                    {
                        if (CheckBoxList1.Items[i].Selected)
                        {
                            // send notification to these people
                            EventControl.invitePeople(Request, CheckBoxList1.Items[i].Value, eventid, eve.EventTitle, Convert.ToInt32(Session["Member"]));
                        }

                    }
                    // invite email

                    String[] address = tbxInviteToEventEmails.Text.Split(new char[] { ';' });

                    foreach (string a in address)
                    {
                        if (a != "")
                        {
                            if ((!MemberControls.IsAccountExist(a)))
                            {
                                Utility.EmailJoinEventMessage(Request, eve.EventTitle, eventid, a);
                            }

                            else
                            {
                                int s = Convert.ToInt32(Session["Member"]);
                                int r = Convert.ToInt32(MemberControls.RetrieveMemberByAccount(a).MembershipNo);
                                EventControl.CreateMemberEvent(eventid.ToString(),r.ToString(), "Member", 3);
                                Utility.SendNotificationForEventInvite(Request, eventid, s, r);
                            }
                        }
                    }
                }
                else
                {
                    error = 100;
                }

            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (error == 0)
                {
                    Session["flag"] = "invited";
                    Response.Redirect("~/ViewEvent.aspx?eventid=" + eventid);
                }
                else if (error == 100)
                { 
                    Session["flag"] = "notfound";
                    Response.Redirect("~/index.aspx");
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "toast", "$().toastmessage('showErrorToast', 'Opps! One of more emails were invalid!');", true);
                }
            }

        }
    }
}